package com.googlecode.ddlutilstool.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.googlecode.ddlutilstool.common.GuiNameAccessor;
import com.googlecode.ddlutilstool.common.ProfileBean;
import com.googlecode.ddlutilstool.kernel.GuiManager;
import com.googlecode.ddlutilstool.kernel.ProfileManager;

/**
 * <p>Class ProfilePanel</p>
 * 
 * <p>On the main window, this contains the list of 
 * Profiles from the ProfileManager.</p>
 * 
 * <p>
 */
@SuppressWarnings("serial")
public class ProfilePanel extends JPanel implements ListSelectionListener {

	private GuiNameAccessor mGuiAccessor;
	private DefaultListModel mProfileListModel;
	private JList mProfileDropDown;

	public ProfilePanel() {
		super();
		mGuiAccessor = new GuiNameAccessor();

		//set the border
		setLayout(new BorderLayout());
		setBorder(new EmptyBorder(5,5,5,5));

		JLabel titleLabel = new JLabel(mGuiAccessor.getString("profile.panel.title"));
		add(titleLabel,BorderLayout.NORTH);
		
		mProfileListModel = new DefaultListModel();
		mProfileDropDown = new JList(mProfileListModel);
		mProfileDropDown.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		mProfileDropDown.addListSelectionListener(this);
		mProfileDropDown.addMouseListener(new MouseHandler());
		mProfileDropDown.setCellRenderer(new ListItemRenderer());
		
		add(mProfileDropDown,BorderLayout.CENTER);
		fillList();
		
	}

	/**
	 * Sets the list as enabled or
	 * disabled.  It is used during DDL execution
	 * to freeze the UI, the unfreeze it.
	 */
	public void setEnabled(boolean pEnabled) {
		super.setEnabled(pEnabled);
		mProfileDropDown.setEnabled(pEnabled);
	}

	/**
	 * Uses the ProfileManager to get the
	 * list of profiles, and displays them
	 * on the list.
	 */
	public void fillList() {
		mProfileDropDown.clearSelection();
		mProfileListModel.clear();
		
		ProfileManager prof = ProfileManager.getInstance();
		Set<String> profiles = prof.getProfileNames();
		for (String name : profiles) {
			ProfileBean profile = prof.getProfile(name);
			mProfileListModel.addElement(profile);
		}
		
	}
	
	/**
	 * Selects the passed in profile as the active
	 * profile, or clears the selection if the
	 * parameter is null.
	 * 
	 * @param pProfile the profile to select, or null to select none.
	 */
	public void setProfile(ProfileBean pProfile) {
		if (pProfile!=null) {
			mProfileDropDown.setSelectedValue(pProfile, true);
		}
		else {
			mProfileDropDown.clearSelection();
		}
	}
	
	/**
	 * @return the currently selected profile from the list.
	 */
	public ProfileBean getProfile() {
		return (ProfileBean)mProfileDropDown.getSelectedValue();
	}
	
	/**
	 * Called because this class implements ListSelectionListener,
	 * it is currently not used.
	 */
	public void valueChanged(ListSelectionEvent e) {
	}
	
	
	/**
	 * From MouseAdapter.  Listens for clicks, and
	 * tries to set the selected prompt as the active
	 * prompt.
	 */
	class MouseHandler extends MouseAdapter{
		public void mouseClicked(MouseEvent e){
			if(e.getClickCount() >= 1) {
				int index = mProfileDropDown.locationToIndex(e.getPoint());
				if (index>=0) {
					ProfileBean profile = (ProfileBean)mProfileListModel.get(index);
					if (profile!=null) {
						setProfile(profile);
						if (e.getClickCount() >= 2) {
							GuiManager.getInstance().editProfile(profile.getName());
						}
					}
					else {
						mProfileDropDown.clearSelection();
					}
				}
			}
		}
	}

	/**
	 * <p>Class ListItemRenderer</p>
	 * 
	 * <p>Displays the profile object in the list, including
	 * the icon, the name, and highlighting the selected profile.</p>
	 */
	class ListItemRenderer extends JLabel implements ListCellRenderer {
		private ImageIcon profileIcon;
		
		public ListItemRenderer() {
			GuiNameAccessor guiAccessor = new GuiNameAccessor();
			profileIcon = new ImageIcon(guiAccessor.getImage("profile.icon"));
		}
		
		public Component getListCellRendererComponent(JList list,Object value,int index,
		boolean isSelected,boolean cellHasFocus) {
			ProfileBean item = (ProfileBean)value;
			setText(item.getName());
	        setIcon(profileIcon);
			if (isSelected) {
				setBackground(list.getSelectionBackground());
				setForeground(list.getSelectionForeground());
			}
			else {
				setBackground(list.getBackground());
				setForeground(list.getForeground());
			}
			setEnabled(list.isEnabled());
			setFont(list.getFont());
			setOpaque(true);
			return this;
		}
	}

}
